#!/bin/sh

. scripts/functions

#
# Run a simple backup 
#   then restore it.
#

unset LANG
TestName="source-addr-test"
JobName=backup

# Directory to backup.
# This directory will be created by setup_data().
# If set, $BackupDirectory will be used
# as base directory by check_restore_diff().
# Don't use a "/" at the end of the directory path,
# as it might cause problems
# (filenames given as parameter to restore must match 100%. "//" are not identical with "/").
BackupDirectory="${tmp}/data"

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data".
setup_data data/small.tgz

scripts/copy-confs

change_jobname BackupClient1FileList $JobName



start_test

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
echo "s%Name = \"$JobName\"%Name = \"$JobName\";  client run after job = \"netstat -an\"%g" >>${outf}
cp ${conf}/susan-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >${conf}/susan-dir.conf

PATH=$PATH:/sbin:/usr/sbin
#
# Deduce a primary IP address by examining
#  the interface defined by INTERFACE=
#
os=`uname`
if [ $os = 'FreeBSD' ]; then
   INTERFACE=xn0
   IP=`ifconfig ${INTERFACE} | perl -ne '/inet (.+?) / && print $1'`
elif [ $os = 'Linux' ]; then
   INTERFACE=$(ip addr show | awk '/inet.*brd/{print $NF; exit}')
   IP=$(ip addr show | awk '/inet.*brd/{print $2; exit}' | sed 's,\(\w\+\)/.*,\1,')
fi

if [ "$IP" = '' ]; then
    # ifconfig output may be like this (RHEL/CentOS 7, Fedora, ...?)
    # eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    #    inet 192.168.121.195  netmask 255.255.255.0  broadcast 192.168.121.255
    IP=`ifconfig ${INTERFACE} | perl -ne '/inet (.+?) / && print $1'`
    if [ "$IP" = '' ]; then
        set_error "Can't detect ip address for interface ${INTERFACE}"
        exit 1
    fi
fi

outf="tmp/sed_tmp"
echo "s/QueryFile/DirSourceAddress=$IP; QueryFile/g" > $outf
echo "s/Address =/Address = 127.0.0.1 #/g" >> $outf
cp ${conf}/susan-dir.conf $tmp/1
sed -f ${outf} ${cwd}/tmp/1 > ${conf}/susan-dir.conf

# Set source address on the FD as well
echo "s/Maximum Concurrent Jobs/FDSourceAddress=$IP; Maximum Concurrent Jobs/g" >$outf
cp ${conf}/susan-fd.conf $tmp/2
sed -f ${outf} ${cwd}/tmp/2 > ${conf}/susan-fd.conf


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
@#label volume=TestVolume001 storage=File pool=Full
@#setdebug level=100 storage=File
run job=$JobName yes
@#sleep 3
messages
quit
END_OF_DATA

run_susan
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label volume=TestVolume001 storage=File pool=Full
@#setdebug level=100 storage=File
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_susan


# sleep 5
print_debug "Detecting connections to SD sourced from ${IP}"
grep -E "127.0.0.1(.|:)$SUSAN_SD_PORT .* ${IP}"   $tmp/log1.out > $tmp/log3.out
if [ $? -ne 0 ]; then
   stat=2
   print_debug "Couldn't detect a connection to the SD sourced from SourceAddress ${IP}"
fi

print_debug "and connections to FD sourced from ${IP}"
grep -E "127.0.0.1(.|:)$SUSAN_FD_PORT .* ${IP}"  $tmp/log1.out >> $tmp/log3.out
if [ $? -ne 0 ]; then
   stat=2
   print_debug "Couldn't detect a connection to the FD sourced from SourceAddress ${IP}"
fi

if [ "$stat" = 2 ] ; then
   netstat -an
fi

if [ "$debug" = 1 ] ; then
    cat $tmp/log3.out
fi


check_two_logs
check_restore_diff

if [ "$stat" = 2 ]; then
    print_debug `cat $tmp/log3.out`
    bstat=$stat
fi

end_test
